﻿using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SmartFrame_DataCollect
{
    public partial class AnalysisRpt : BaseModule
    {
        public AnalysisRpt()
        {
            InitializeComponent();
            this.bgDateEdit1.Properties.Mask.EditMask = "yyyy年MM月";
            this.bgDateEdit1.Properties.VistaCalendarInitialViewStyle = DevExpress.XtraEditors.VistaCalendarInitialViewStyle.YearView;
            this.bgDateEdit1.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView;

        }

        private void btnSearch1_Click(object sender, EventArgs e)
        {
            string productNo = "";
            if (editProductNo.EditValue != null)
            {
                productNo = editProductNo.EditValue.ToString();
            }
            string equipNo = "";
            if (editEquipNo.EditValue != null)
            {
                equipNo = editEquipNo.EditValue.ToString();
            }
            string bgTime = "";
            if (bgDateEdit1.EditValue != null)
            {
                bgTime = bgDateEdit1.EditValue.ToString();
            }
            // 确保使用的是BandedGridView而不是普通的GridView
            BandedGridView bandedGridView = gridControl1.MainView as BandedGridView;
            if (bandedGridView == null)
            {
                gridControl1.MainView = new BandedGridView(gridControl1);
                bandedGridView = gridControl1.MainView as BandedGridView;
            }

            //本地数据库
            //绑定数据源
            DataSet dsResult = DbHelperSQL.Query("exec [dbo].[P_Get_CJ_Record_new_5] @ProductCode,@FBDate", "Data Source=.;Initial Catalog=ST1_TongGuan3CJ;User Id=sa;Password=knj123456", new SqlParameter[] {
                new SqlParameter("ProductCode",productNo),
                new SqlParameter("FBDate",(bgTime==""?"":DateTime.Parse(bgTime).ToString("yyyy-MM-dd")) )
            });

            //DataSet dsResult = DbHelperSQL.Query(" select 1 as d ");


            // 清除现有设置
            bandedGridView.Bands.Clear();
            //报表列头数据源
            string headSql = @"select label,name,parentName,width,total,sortIndex,isFrozen from
                                (
                                select '产品编号' as label, '公司' as name, '' as parentName, 110 as width, '' as total, 0 as sortIndex, 0 as isFrozen
                                union
                                select '品质结果' as label, '品质结果' as name, '' as parentName, 110 as width, '' as total, 1 as sortIndex, 0 as isFrozen
                                union
                                select '自动超声套圈' as label, '自动超声套圈' as name, '' as parentName, 110 as width, '' as total, 101 as sortIndex, 0 as isFrozen
                                union
                                select '峰值频率' as label, 'A段振幅' as name, '自动超声套圈' as parentName, 110 as width, '' as total, 5 as sortIndex, 0 as isFrozen
                                union
                                select '超声时间' as label, '超声时间' as name, '自动超声套圈' as parentName, 110 as width, '' as total, 6 as sortIndex, 0 as isFrozen
                                union
                                select '超声能量1（双字）' as label, '超声能量1（双字）' as name, '自动超声套圈' as parentName, 110 as width, '' as total, 7 as sortIndex, 0 as isFrozen
                                union
                                select '超声品质' as label, '实际超声品质' as name, '自动超声套圈' as parentName, 110 as width, '' as total, 10 as sortIndex, 0 as isFrozen
                                union
                                select 'CCD品质' as label, 'CCD品质' as name, '' as parentName, 110 as width, '' as total, 102 as sortIndex, 0 as isFrozen
                                union
                                select 'CCD检测品质' as label, '实际CCD检测品质' as name, 'CCD品质' as parentName, 110 as width, '' as total, 4 as sortIndex, 0 as isFrozen
                                union
                                select '透气量测试仪2Kpa' as label, '透气量测试仪2Kpa' as name, '' as parentName, 110 as width, '' as total, 103 as sortIndex, 0 as isFrozen
                                union
                                select '测试结果' as label, '实际测试结果_52' as name, '透气量测试仪2Kpa' as parentName, 110 as width, '' as total, 13 as sortIndex, 0 as isFrozen
                                union
                                select '测试压力' as label, '测试压力_52_CH' as name, '透气量测试仪2Kpa' as parentName, 110 as width, '' as total, 15 as sortIndex, 0 as isFrozen
                                union
                                select '透气量' as label, '透气量_52' as name, '透气量测试仪2Kpa' as parentName, 110 as width, '' as total, 16 as sortIndex, 0 as isFrozen
                                union
                                select '透气量测试仪7Kpa' as label, '透气量测试仪7Kpa' as name, '' as parentName, 110 as width, '' as total, 104 as sortIndex, 0 as isFrozen
                                union
                                select '测试结果' as label, '实际测试结果_53' as name, '透气量测试仪7Kpa' as parentName, 110 as width, '' as total, 19 as sortIndex, 0 as isFrozen
                                union
                                select '测试压力' as label, '测试压力_53_CH' as name, '透气量测试仪7Kpa' as parentName, 110 as width, '' as total, 21 as sortIndex, 0 as isFrozen
                                union
                                select '透气量' as label, '透气量_53' as name, '透气量测试仪7Kpa' as parentName, 110 as width, '' as total, 22 as sortIndex, 0 as isFrozen
                                --union
                                -- select '运行状态' as label, '实际运行状态_52' as name, '透气量测试仪2Kpa参数' as parentName, 110 as width, '' as total, 23 as sortIndex, 0 as isFrozen
                                --union
                                -- select '运行状态' as label, '实际运行状态_53' as name, '透气量测试仪7Kpa参数' as parentName, 110 as width, '' as total, 34 as sortIndex, 0 as isFrozen


                                ) t order by sortIndex ";
            DataTable dtCol = DbHelperSQL.Query(headSql).Tables[0];



            foreach (DataRow drCol in dtCol.Rows)
            {
                if (drCol["parentName"].ToString() == "")
                {
                    GridBand gridBand = new GridBand()
                    {
                        Caption = drCol["label"].ToString(),
                        Name = drCol["name"].ToString(),
                        Visible = true
                    };
                    List<DataRow> listChildren = dtCol.AsEnumerable().Where(x => x["parentName"].ToString() == drCol["name"].ToString()).ToList();
                    if (listChildren.Count > 0)
                    {
                        int i = 0;
                        foreach (DataRow drChildCol in listChildren)
                        {
                            if (drChildCol["label"] != null && drChildCol["name"] != null)
                            {
                                BandedGridColumn gridColumn = new BandedGridColumn()
                                {
                                    Caption = drChildCol["label"].ToString(),
                                    FieldName = drChildCol["name"].ToString(),
                                    Visible = true,
                                    Width = int.Parse(drChildCol["width"].ToString())
                                };

                                //gridColumn.ColumnType.
                                gridBand.Columns.Add(gridColumn);
                                bandedGridView.Columns.Add(gridColumn);
                            }

                        }
                        bandedGridView.Bands.Add(gridBand);
                    }
                    else
                    {
                        BandedGridColumn gridColumn = new BandedGridColumn()
                        {
                            Caption = drCol["label"].ToString(),
                            FieldName = drCol["name"].ToString(),
                            Visible = true,
                            Width = int.Parse(drCol["width"].ToString())
                        };

                        //gridColumn.ColumnType.
                        gridBand.Columns.Add(gridColumn);
                        bandedGridView.Columns.Add(gridColumn);
                        bandedGridView.Bands.Add(gridBand);
                    }


                    //bandedGridView.Columns.Add(new gridcol()
                    //{

                    //});
                }

            }


            if (dsResult.Tables.Count > 0)
            {
                gridControl1.DataSource = dsResult.Tables[0];
            }


            // 确保使用的是BandedGridView而不是普通的GridView
            //BandedGridView bandedGridView = gridControl1.MainView as BandedGridView;
            //if (bandedGridView == null)
            //{
            //    gridControl1.MainView = new BandedGridView(gridControl1);
            //    bandedGridView = gridControl1.MainView as BandedGridView;
            //}
            //GridBand gb = new GridBand()
            //{
            //    Caption = "1",
            //    Name = "t",
            //    Visible = true
            //};
            //BandedGridColumn c1 = new BandedGridColumn()
            //{
            //    Caption = "列1",
            //    Name = "c1",
            //    Visible = true,
            //    FieldName = "c1",
            //    Width = 200
            //};
            //t.Columns.Add(c1);
            //bandGridView1.Columns.Add(c1);

        }
    }
}
